﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CipherBox;
using System.IO;
using System.Diagnostics;
using AppLimit.CloudComputing.SharpBox;
using System.Runtime.InteropServices;
using System.Resources;
using System.Reflection;
using CipherBox.Properties;

namespace CipherBox
{
    public delegate void DropBoxProgressChanged(string text, ToolTipIcon toolTipIcon = ToolTipIcon.Info);

    public partial class SynchronisationDialog : Form
    {
        #region Win32
        const int AW_HIDE = 0X10000;
        const int AW_ACTIVATE = 0X20000;
        const int AW_SLIDE = 0X40000;
        const int AW_BLEND = 0X80000;
        const int AW_HOR_POSITIVE = 0X1;
        const int AW_HOR_NEGATIVE = 0X2;
        const int AW_VER_POSITIVE = 0x4;
        const int AW_VER_NEGATIVE = 0x8;
        const int AW_CENTER = 0x10;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int AnimateWindow(IntPtr hwand, int dwTime, int dwFlags);
        #endregion
        public DropBoxProgressChanged _OnDropBoxProgressChanged;
        public DropBoxStateChanged _OnDropBoxStateChanged;
        public FileOperationProgressChanged _OnTransferProgressChanged;
        settings m_Settings;
        string SETTINGS_PATH = Application.StartupPath + "\\settings.xml";
        public SynchronisationDialog()
        {
            InitializeComponent();
        }
        private void OnDropBoxStateChanged(TransferType t)
        {
            switch (t)
            {
                case (TransferType.DeleteLocal):
                case (TransferType.DeleteRemote):
                    notifyIcon1.Icon = Resources.delete;
                    break;
                case (TransferType.Download):
                    notifyIcon1.Icon = Resources.download;
                    break;
                case (TransferType.Pause):
                    notifyIcon1.Icon = Resources.pause;
                    break;
                case (TransferType.Upload):
                    notifyIcon1.Icon = Resources.upload;
                    break;
            }
        }
        public void OnDropBoxProgressChanged(string text, ToolTipIcon toolTipIcon = ToolTipIcon.Info)
        {
            notifyIcon1.ShowBalloonTip(4, "CipherBox", text, toolTipIcon);
        }
        DropBoxSynchronisation m_DropBoxSynchronisation;
        private void loadSettings()
        {
            try
            {
                m_Settings = XMLSerializing.Deserialize<settings>(SETTINGS_PATH);
            }
            catch (FileNotFoundException)
            {
                m_Settings = new settings();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            AnimateWindow(this.Handle, 1000, AW_ACTIVATE | AW_BLEND);
            loadSettings();
            List<ToolStripMenuItem> l = new List<ToolStripMenuItem>();
            foreach (settingsJob j in m_Settings.job)
            {
                ToolStripMenuItem i = new ToolStripMenuItem(Path.GetDirectoryName(j.localPath));
                i.Size = new System.Drawing.Size(226, 22);
                i.Text = j.localPath.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Last<string>();
                i.Click += delegate(System.Object o, EventArgs ea)
                 { openFolder(j.localPath); };
                toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { i });
            }
            backgroundWorker1.RunWorkerAsync();
        }
        private void openFolder(string path)
        {
            Process p = new Process();
            p.StartInfo.Arguments = path;
            p.StartInfo.FileName = "explorer";
            p.Start();
        }

        private void einstellungenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsDialog s = new SettingsDialog();
            s.ShowDialog();
            loadSettings();
        }
        private void openSettings()
        {
            Process p = new Process();
            p.StartInfo.FileName = "settings.exe";
            p.Start();
            p.WaitForExit();
            loadSettings();
        }

        private void openDropBoxWebsiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = "https://www.dropbox.com/home";
            p.Start();
        }
        private void start()
        {
            _OnDropBoxProgressChanged = this.OnDropBoxProgressChanged;
            _OnDropBoxStateChanged = this.OnDropBoxStateChanged;
            _OnTransferProgressChanged = new FileOperationProgressChanged(delegate(System.Object o, FileDataTransferEventArgs a)
            {

                notifyIcon1.Text = "Datei wird " + ((m_DropBoxSynchronisation.CurrentType == TransferType.Download) ? "herunter" : "hoch") + "geladen: " + a.PercentageProgress + "%";
                dateiWirdHeruntergeladenToolStripMenuItem.Text = "Datei wird heruntergeladen: " + a.PercentageProgress + "%";
            });
            m_DropBoxSynchronisation = new DropBoxSynchronisation(m_Settings, _OnTransferProgressChanged, _OnDropBoxProgressChanged, OnDropBoxStateChanged);
            //   backgroundWorker1.RunWorkerAsync();
            dateiWirdHeruntergeladenToolStripMenuItem.Text = "Eingehende Synchronisation..";
            notifyIcon1.Text = "Eingehende Synchronisation..";
            m_DropBoxSynchronisation.SyncCleanOutgoing();
            m_DropBoxSynchronisation.SyncIncoming();
            dateiWirdHeruntergeladenToolStripMenuItem.Text = "Ausgehende Synchronisation..";
            notifyIcon1.Text = "Ausgehende Synchronisation..";
            m_DropBoxSynchronisation.SyncCleanIncoming();
            m_DropBoxSynchronisation.SyncOutgoing();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            start();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
        int counter = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (counter++ > 50)
            {
                AnimateWindow(this.Handle, 1000, AW_HIDE | AW_BLEND);
                timer1.Enabled = false;
            }

        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void SynchronisationDialog_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

    }
}
